package com.symone.webcms.controller.base;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.symone.webcms.common.Constants;
import com.symone.webcms.model.*;
import com.symone.webcms.model.vo.PermissionVO;
import com.symone.webcms.model.vo.RoleVO;
import com.symone.webcms.model.vo.UserVO;
import com.symone.webcms.service.ConfigService;
import com.symone.webcms.service.MenuActionService;
import com.symone.webcms.service.MenuService;
import com.symone.webcms.util.SnowflakeUtil;
import com.symone.webcms.util.TreeUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ConfigService configService;

    public HttpServletRequest getRequest(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }
    /**
     * 根据请求heard中的token获取用户ID
     *
     * @return 用户ID
     */
    public UserVO getSessionUser() {
        return (UserVO) SecurityUtils.getSubject().getPrincipal();
    }

    public Session getSession() {
        return  SecurityUtils.getSubject().getSession();
    }

//    public Menu getSessionCurrentMenu(){
//        getRequest().getRequestURI();
//    }

    public Set<PermissionVO> getSessionPermissionList() {
        Set<PermissionVO> permissions = (Set<PermissionVO>) getSession().getAttribute(Constants.CURRENT_PERMISSION_SESSION);
        return permissions;
    }
    public String getMvcFormCheck(){
        long uuid = SnowflakeUtil.getInstance().nextId();
        getSession().setAttribute(Constants.FORM_CHECK_SESSION, uuid);
        return Long.toString(uuid);
    }

    public boolean checkMvcForm(Long formcheck){
        Long session_formcheck = (Long)getSession().getAttribute(Constants.FORM_CHECK_SESSION);
        if(session_formcheck.equals(formcheck) ){
            return true;
        }
        return false;
    }

    /**
     * 根据请求heard中的token获取用户组
     *
     * @return 角色
     */
    public Set<RoleVO> getSessionRoleList() {
        return getSessionUser().getRoleList();
    }

    public List<Config> getSessionConfigList(String groupName){
        return configService.selectList(new EntityWrapper<Config>().where("deleted = 0 and code like CONCAT('"+groupName+"','%' ) "));
    }

    public boolean checkPermission(String shiroCode){
        for(PermissionVO pm : getSessionPermissionList()){
            if(pm.getCode().equals(shiroCode)){
                return true;
            }
        }
        return false;
    }

    public List<Menu> getSessionMenuList() {
        List<Menu> menus = (List<Menu>) getSession().getAttribute(Constants.CURRENT_MENU_LIST);
        menus = new TreeUtil<Menu>().bulid(menus);
        return menus;
    }

    public List<Language> getSessionLanguageList(){
        List<Language> languages = new Language().selectList(new EntityWrapper().eq("deleted",0));
        getSession().setAttribute(Constants.SYS_LANGUAGE_LIST,languages);
        return languages;
    }

    public Language getLanguageById(Integer id){
        List<Language> languages = getSessionLanguageList();
        for (Language lang : languages){
            if(lang.getId() == id){
                return lang;
            }
        }
        return null;
    }

    public Language getSessionLanguage(){
        return (Language) getSession().getAttribute(Constants.CURRENT_LANGUAGE);
    }

}
